jQuery(document).ready(function($) {
    $("#regempform").validate({
       rules: {
          id_comp_type: { required: true },
          id_industry: { required: true },
          id_state: { required: true },
          id_city: { required: true },
          id_size_type: { required: true },
          phone: { required: true, phone: true},
          contact_name: { required: true ,notalldigits : true },
          address: { required: true }
       }, 

       messages: {
          id_comp_type: {
             required: "请选择公司性质"
          },
          id_industry: {
              required: "请选择公司行业"
          },
          id_size_type: {
              required: "请选择公司规模"
          },
          contact_name: {
             notalldigits: "联系人不能为纯数字"
          },
          phone: {
             phone: "请输入正确的联系电话"
          }
       }, 
       submitHandler: function(form) {
          $(form).ajaxSubmit({
            dataType: "json", 
            beforeSubmit: function(data, form, options) { 
                if(!showLoading("regempform"))
                   return false;
                $(".error.server", form).hide();
             },
             success: function(data) {
               try {
                  var retmsg = data; 
                  if(retmsg.error) { $(".error.server", form).jobUtil("showError", retmsg.error); }
                  else if(retmsg.success) {
                      $(".error.server", form).jobUtil("showSuccess", retmsg.success).
                         fadeIn().delay(200, function() { 
                            window.location = companyhome; 
                      }); 
                      
                  }
                      
               } catch(e) {
                  return false;
               }
             }
         });
       }

   });
   // re-size the table td key label field for alignment
   $("label.key", $("#regempform")).each(function(i, el) { beautifyTableCaption(el); });

   /* show province/city selection */
   $("#regempform #id_state").jobLocation({
       'getcityurl': getcityurl,
       'getprovinceurl': getprovinceurl
   });
   
   $('#regempform').on('click','#industry_name,#selectindustry',function(){
      var target =  $(this);
      $.ajax({
        type:'GET',
        url:OC.filePath('company', 'ajax', 'getindustry.php'),
        dataType: 'html',
        data:{id_industry:$("#id_industry").val()},
        beforeSend: function(xhr, settings) {
           showLoading(target, settings, null, {button:true});
        },
        success: function(result) {
           $("#selectindustry_dialog .modal-body").html(result);
           // modal changed from fix to absolute, so the dialog is only on top part of view window -- need to be adjusted to be centered 
           var scrolltop = $(window).scrollTop();
           $("#selectindustry_dialog").css({top:scrolltop+154}).modal('show');
        }
      });
      return false;
   })
   // focus first text-field 
   //setFocus("regempform");
});
